package org.fireflow2.designer.eclipse.properties.tab;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;

public class CommonAttributePropertySection extends AbstractPropertySection {

	AbstractEditPart editPart = null;
	ModelWrapper modelWrapper = null;

	private Text idText;
	private Text nameText;
	private Text displayNameText;
	private Text descriptionText;

	// private CCombo readOnlyCombo;

	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		
		
		Composite composite = getWidgetFactory()
				.createFlatFormComposite(parent);

		composite.setLayout(new GridLayout(2, false));

		// id
		getWidgetFactory().createCLabel(composite, "Id: ");

		idText = getWidgetFactory().createText(composite, "");
		idText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		idText.setEditable(false);

		// name
		getWidgetFactory().createCLabel(composite, "名称: ");

		nameText = getWidgetFactory().createText(composite, "");
		nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		nameText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				if(nameText.getText()==null || nameText.getText().trim().equals("")){
					MessageDialog.openInformation(
							null,
							"错误",
							"名称不能为空！");		
					nameText.setFocus();
					return;
				}
				String oldName = modelWrapper.getName();
				if (!oldName.trim().equals(nameText.getText().trim())){
					if (modelWrapper.isTheNameConflict(nameText.getText().trim())){
						MessageDialog.openInformation(
								null,
								"错误",
								"名称冲突! 名称为 [\""+nameText.getText() +"\"] 的元素已存在");		
						nameText.setFocus();
						return;
					}
					modelWrapper.setName(nameText.getText().trim());
				}
			}

		});

		//displayname
		getWidgetFactory().createCLabel(composite, "显示名称: ");

		displayNameText = getWidgetFactory().createText(composite, "");
		displayNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		displayNameText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				modelWrapper.setDisplayName(displayNameText.getText());
			}

		});
		
		//description
		getWidgetFactory().createCLabel(composite, "描述: ");

		descriptionText = getWidgetFactory().createText(composite, "",SWT.MULTI);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.verticalSpan =3;
		descriptionText.setLayoutData(gridData);
		descriptionText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				modelWrapper.setDescription(descriptionText.getText());
			}

		});		
	}

	@Override
	public void refresh() {
		super.refresh();
		String id = modelWrapper.getID();
		this.idText.setText(id==null?"":id);
		
		String name = modelWrapper.getName();
		this.nameText.setText(name==null?"":name);
		
		String displayName = modelWrapper.getDisplayName();
		this.displayNameText.setText(displayName==null?"":displayName);
		
		String description = modelWrapper.getDescription();
		this.descriptionText.setText(description==null?"":description);

	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		
		Object obj = ((IStructuredSelection) selection).getFirstElement();
//		System.out.println("=============-----SetInput-------------");
		if (obj instanceof AbstractEditPart) {
			editPart = (AbstractEditPart) obj;
			modelWrapper = (ModelWrapper) editPart.getModel();
		}
	}
}
